#include "libtimers.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>



void cb(void *arg1, uint32_t tid)
{
    printf("Times up tid: %d id: %d\n", tid, (int)arg1);
    return;
}

void test1()
{
    LT_TIMERPOOL *pool = lt_new(NULL);
    srand(time(NULL));

    uint64_t start_t = get_now_time_ms();
    for(int i = 0; i < 1000000; i ++) {
        int i = rand() % 10000;
        //uint64_t now = get_now_time_ms();
        uint32_t tid = lt_timer_open(pool);
        //lt_set_timer(pool, tid, (void*)i + (now - start_t), i);
        lt_set_timer(pool, tid, (void*)(i * 10), i);
    }
    uint64_t now = get_now_time_ms();
    printf("Add 1,000,000 timers cost: %d ms\n", now - start_t);

    LT_TIMER_EVENT *events;

    while(lt_timer_size(pool)) {
        usleep(10000);
        int event_count = lt_poll(pool, &events);
        printf(" poll get %d events \n", event_count);
        for(int i = 0; i < event_count; ++i)
            //cb(events[i].data, events[i].timer_id);
            lt_timer_close(pool, events[i].timer_id);
        lt_free_events(pool, events);
    }

    lt_destroy(pool);
    return 0;
}

void test2()
{
    uint64_t nouse = 0;
    uint64_t start_t = get_now_time_ms();
    for(int i = 0; i < 100000; i++) {
        nouse += get_now_time_ms();
        nouse += get_now_time_ms();
    }
    uint64_t now = get_now_time_ms();
    printf("get_now_time_ms() 1,000,000 timers cost: %d ms\n", now - start_t);
}

int main(int argc, char *argv[])
{
    test1();
    //test2();
    //test3();
}




